package kdweibo4j.http;

import java.net.HttpURLConnection;
import java.util.HashMap;
import java.util.Iterator;

import com.kdweibo.utils.Log;



public class HttpConnectionManager {
	private static HttpConnectionManager instance = null;
	//连接列表
    private HashMap<String, HttpURLConnection> connMap= new HashMap<String, HttpURLConnection>();
    
    public static HttpConnectionManager getInstance(){
    	if(instance==null){
    		instance = new HttpConnectionManager();
    	}
    	return instance;
    }
    
    public void addConn(String connType, HttpURLConnection conn){
    	this.connMap.put(connType, conn);
    }
    
    
    public void romveConn(String connType){
    	HttpURLConnection conn = this.connMap.get(connType);
    	if(conn!=null){
    		try{
    			conn.disconnect();
    		}catch(Exception e){
    			Log.logerror("romveConn", "Force disconnect a http connection catch an error,ignroe it!",e);
    		}finally{
    			connMap.remove(connType);
    		}
    	}
    }
    
    /**
     * 断开正在连接中的所有连接，当前操作优先级最高时调用此方法。
     */
    public void removeAllConnect(){
    	Iterator<String> iter = connMap.keySet().iterator();
    	while(iter.hasNext()){
    		String connType = iter.next();
    		romveConn(connType);
    	}
    }
    
}
